<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Moffline extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getIndex($number = 0,$offset = 0,$cat = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        if($cat == 0)
        {
            $sql = "SELECT * FROM off_questions ORDER BY id DESC";    
        }else{
            $sql = "SELECT * FROM off_questions WHERE categories_id = '".$cat."' ORDER BY id DESC";
        }
        $count = $this->db->query($sql)->num_rows();
        if($offset >= $count && $count != 0){
            $this->load->view('template/404',$this->data);
            return false;
        }
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_array();
        return array('data' => $xs, 'count' => $count);
    }
 
    public function getQuestionCate()
    {
        $sql = "SELECT id,name FROM off_questions_categories";
        $query = $this->db->query($sql);
        $data = $query->result_array();
        
        $arr = array();
        foreach($data AS $key => $value)
        {
            $arr[$value['id']] = $value['name'];
        }
        return $arr; 
    }
    
	public function list_question()
    {
        $sql = "SELECT content FROM off_questions";
        $query = $this->db->query($sql);
        $data = ($query->result_array());
        
        $arr = array();
        foreach($data AS $key => $value)
        {
			$temp = json_decode($value['content']);
			$temp = $temp->question;
			array_push($arr,$temp);
        }
		$result = json_encode($arr);
		return $result; 
    }
	
    public function addThisQuestion($cat_quest,$question,$choice1,$choice2,$choice3,$choice4,$answer)
    {
        $q = array('question' => $question);
        $x = array('choice' => $as = array(
            '1' => $choice1,
            '2' => $choice2,
            '3' => $choice3,
            '4' => $choice4
        ));
        $arr = json_encode(array_merge($q, $x));
        
        $data = array(
            'content' => $arr,
            'answer' => $answer,
            'categories_id' => $cat_quest,
            'created' => date('Y-m-d H:i:s')
        );
        //echo '<pre>';
        //die(print_r($arr));
        $this->db->insert('off_questions', $data);
        return 200;
    }
    
    public function getThisQuestion($id)
    {
        $data = $this->db->query("SELECT * FROM off_questions WHERE id = '".$id."'")->row_array();
        return $data;
    }
    public function editThisQuestion($cat_quest,$id,$question,$choice1,$choice2,$choice3,$choice4,$answer)
    {
        $q = array('question' => $question);
        $x = array('choice' => $as = array(
            '1' => $choice1,
            '2' => $choice2,
            '3' => $choice3,
            '4' => $choice4
        ));
        $arr = json_encode(array_merge($q, $x));
        
        $data = array(
            'content' => $arr,
            'answer' => $answer,
            'categories_id' => $cat_quest,
            'created' => date('Y-m-d H:i:s')
        );
        $this->db->update('off_questions', $data, "id = '".$id."'");
        return 200;
    }
    
    public function deleteThisQuestion($id)
    {
        $this->db->delete('off_questions', "id = '".$id."'");
    }
	
	public function check_content_question($question)
    {
		$question = addslashes($question); // bỏ dấu stripslashes
		$question = json_encode($question);
		$question = str_replace('"','',$question);
		//return $question;
        $data = $this->db->query("SELECT content FROM off_questions WHERE content LIKE  '%$question%' ")->row_object();
        return "SELECT content FROM off_questions WHERE content LIKE  '%$question%' ";
		//return $data->content;
    }
	
	
}
